<template>
  <div class="community-statistics-container">
    <h2 class="page-title">社区统计</h2>
    
    <el-row :gutter="20" class="summary-cards">
      <el-col :xs="24" :sm="12" :md="6">
        <el-card class="stat-card">
          <div class="card-content">
            <div class="card-icon post-icon">
              <el-icon><Document /></el-icon>
            </div>
            <div class="card-info">
              <p class="card-label">帖子总数</p>
              <p class="card-value">{{ statistics.totalPosts || 0 }}</p>
            </div>
          </div>
        </el-card>
      </el-col>

      <el-col :xs="24" :sm="12" :md="6">
        <el-card class="stat-card">
          <div class="card-content">
            <div class="card-icon comment-icon">
              <el-icon><ChatDotRound /></el-icon>
            </div>
            <div class="card-info">
              <p class="card-label">评论总数</p>
              <p class="card-value">{{ statistics.totalComments || 0 }}</p>
            </div>
          </div>
        </el-card>
      </el-col>

      <el-col :xs="24" :sm="12" :md="6">
        <el-card class="stat-card">
          <div class="card-content">
            <div class="card-icon like-icon">
              <el-icon><ChatDotRound /></el-icon>
            </div>
            <div class="card-info">
              <p class="card-label">点赞总数</p>
              <p class="card-value">{{ statistics.totalLikes || 0 }}</p>
            </div>
          </div>
        </el-card>
      </el-col>

      <el-col :xs="24" :sm="12" :md="6">
        <el-card class="stat-card">
          <div class="card-content">
            <div class="card-icon today-icon">
              <el-icon><Document /></el-icon>
            </div>
            <div class="card-info">
              <p class="card-label">今日新增</p>
              <p class="card-value">{{ statistics.todayNewPosts || 0 }}</p>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <el-card class="chart-card">
      <template #header>
        <div class="card-header">帖子发布趋势</div>
      </template>
      <div ref="postChart" class="chart"></div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted, nextTick } from 'vue'
import { ElMessage } from 'element-plus'
import { Document, ChatDotRound } from '@element-plus/icons-vue'
import * as echarts from 'echarts'
import request from '@/utils/request'

const statistics = ref({})
const postChart = ref(null)

const getStatistics = async () => {
  try {
    const response = await request.get('/admin/dashboard/summary')
    if (response.code === 0) {
      statistics.value = {
        totalPosts: response.data.totalPosts || 0,
        totalComments: response.data.totalComments || 0,
        totalLikes: response.data.totalLikes || 0,
        todayNewPosts: response.data.todayNewPosts || 0
      }
    }
  } catch (error) {
    console.error('获取社区统计失败', error)
    ElMessage.error('获取社区统计失败')
  }
}

const initChart = async () => {
  try {
    const response = await request.get('/admin/dashboard/postTrend')
    if (response.code === 0 && postChart.value) {
      const data = response.data || []
      const dates = data.map(item => item.date)
      const counts = data.map(item => item.count)

      const chart = echarts.init(postChart.value)
      chart.setOption({
        tooltip: {
          trigger: 'axis'
        },
        xAxis: {
          type: 'category',
          data: dates
        },
        yAxis: {
          type: 'value'
        },
        series: [{
          data: counts,
          type: 'bar',
          itemStyle: {
            color: '#67C23A'
          }
        }]
      })

      window.addEventListener('resize', () => {
        chart.resize()
      })
    }
  } catch (error) {
    console.error('获取帖子趋势失败', error)
  }
}

onMounted(async () => {
  await getStatistics()
  await nextTick()
  initChart()
})
</script>

<style scoped>
.community-statistics-container {
  padding: 20px;
}

.page-title {
  margin: 0 0 24px 0;
  font-size: 24px;
  font-weight: 600;
  color: #333;
}

.summary-cards {
  margin-bottom: 20px;
}

.stat-card {
  border-radius: 8px;
}

.card-content {
  display: flex;
  align-items: center;
  gap: 20px;
}

.card-icon {
  width: 60px;
  height: 60px;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 28px;
  color: #fff;
}

.post-icon {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.comment-icon {
  background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
}

.like-icon {
  background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
}

.today-icon {
  background: linear-gradient(135deg, #43e97b 0%, #38f9d7 100%);
}

.card-info {
  flex: 1;
}

.card-label {
  margin: 0;
  font-size: 14px;
  color: #999;
}

.card-value {
  margin: 8px 0;
  font-size: 28px;
  font-weight: bold;
  color: #333;
}

.chart-card {
  border-radius: 8px;
}

.card-header {
  font-size: 16px;
  font-weight: 600;
  color: #333;
}

.chart {
  width: 100%;
  height: 400px;
}
</style>

